package com.echo.timeSeries;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Random;

import com.echo.timeSeries.BallTest.Ball;


import processing.core.PApplet;
import traer.physics.Particle;
import traer.physics.ParticleSystem;

public class Demo extends PApplet{
	ParticleSystem physics;
	Random ran = new Random();
	ArrayList elements = null;
	ArrayList tempList = null;
	int ballWidth = 48;
	int i=0;

	Particle p;

	
	// Simple bouncing ball class

	public class Ball {
	  
	  float x;
	  float y;
	  float speed;
	  float gravity;
	  float w;
	  float life = 255;
	  String id;
	  Date activeTime;
	  
	  Ball(float tempW, String tempId, Date tempTime) {
	    x = (float)0.0;
	    y = (float)0.0;
	    w = tempW;
	    id = tempId;
	    activeTime = tempTime;
	    speed = 0;
	    gravity = (float)0.1;
	  }
	  
	    void move() {
	    // Add gravity to speed
	    speed = speed + gravity;
	    // Add speed to y location
	    y = y + speed;
	    // If square reaches the bottom
	    // Reverse speed
	    if (y > height) {
	      // Dampening
	      speed = speed * (float)-0.8;
	      y = height;
	    }
	  }
	  
	  boolean finished() {
	    // Balls fade out
	    life--;
	    if (life < 0) {
	      return true;
	    } else {
	      return false;
	    }
	  }
	  
	  void display(float x, float y, float r, float g, float b) {
		  
		fill(r, g, b);
	    //stroke(0,life);
	    ellipse(x,y,w,w);
	  }
	} 

	public void setup()
	{
	  size( 200, 200 );
	  smooth();
	  ellipseMode(CENTER);
	  noStroke();
	  elements = new ArrayList<Ball>();
	        
	  try{
		  getDataFromFile();
	  }catch(IOException e){
		  e.printStackTrace();
	  }
	  
	  tempList = new ArrayList<Ball>();
	  tempList.add(elements.get(0));
	  
	}

	
	void getDataFromFile()throws IOException{
		FileInputStream fis = null;
		BufferedInputStream bis = null;
		DataInputStream dis = null;
		String[] temp = null;
		File dataFile = new File(this.getClass().getResource("/").getPath()+"\\com\\echo\\data\\Data09-7-12 135912.txt");
		fis = new FileInputStream(dataFile);
		bis = new BufferedInputStream(fis);
		dis = new DataInputStream(bis);
		StringBuffer sb = new StringBuffer();
		while(dis.available()!=0){
			sb = new StringBuffer(dis.readLine());
			temp = sb.toString().split(" 1 ");
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date = null;
			try{
				date = format.parse(temp[1]);
			}catch(ParseException e){
				e.printStackTrace();
			}
			elements.add(new Ball(ballWidth, temp[0], date));
			System.out.println(date.toString());
		}
	}

	public void draw()
	{
	  background( 255 );
	  for (int i = tempList.size()-1; i > 0; i--) { 
		    // An ArrayList doesn't know what it is storing so we have to cast the object coming out
		    Ball ball = (Ball) elements.get(i);
		    ball.move();
		    if(((Ball)elements.get(i-1)).id.equals(ball.id)){
		    	ball.display(ball.x, ball.y, i-1, i-1, i-1);
		    }else{
		    	ball.display(ball.x, ball.y, ran.nextInt(255), ran.nextInt(255), ran.nextInt(255));
		    }
		    if (ball.finished()) {
		      // Items can be deleted with remove()
//		      tempList.remove(i);
		    }
		    
	 }
	 
	}
	
	public void mousePressed() {
		  // A new ball object is added to the ArrayList (by default to the end)
		i++;
		Ball tempBall = (Ball)elements.get(i);
		tempBall.x = mouseX;
		tempBall.y = mouseY;
		tempList.add(tempBall);
	}
	
	
	
	
}
